<template>
  <div class="login-main">
    <el-form
      :model="form"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="login-container"
    >
      <h3 class="login-title">登录</h3>
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="form.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :loading="isLoading"
          @click="login"
          :disabled="isButtonDisabled"
          >Login</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";
import Cookie from "js-cookie";
import { Message } from "element-ui";

export default {
  name: "Login",
  data() {
    return {
      isLoading: false,
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
            { required: true, message: "请输入密码", trigger: "blur" }
        ],
      },
    };
  },
  computed: {
    isButtonDisabled() {
      return !this.form.username || !this.form.password;
    },
  },
  methods: {
    login() {
        this.isLoading = true;

        axios
          .post("http://114.67.241.121:8088/user/login", null, {
            params: {
              username: this.form.username,
              password: this.form.password,
            },
          })
          .then((res) => {
            console.log(res.data);
            if (res.data.success === true) {
              Message.success(res.data.msg);
              Cookie.set("token", res.data.data.token);
              this.$router.push({ name: "home" });
            } else {
              Message.error(res.data.msg);
            }

            this.isLoading = false;
          })
          .catch((err) => {
            console.log(err);
          });
    
    },
    }
}
</script>
<style lang='less' scoped>
.login-main {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-container {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    width: 350px;
    box-shadow: 0.4px 0.2px 10px #ccc;
    border-radius: 10px;
    padding: 30px;
    justify-content: center;

    .login-title {
      font-size: 1.5rem;
      text-align: center;
    }
  }
}
</style>        